package com.dayu.web.controller.share;

import java.util.List;

import com.dayu.share.domain.ShareGoods;
import com.dayu.share.service.IShareGoodsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dayu.common.annotation.Log;
import com.dayu.common.core.controller.BaseController;
import com.dayu.common.core.domain.AjaxResult;
import com.dayu.common.enums.BusinessType;
import com.dayu.share.domain.ShareGoodsBrand;
import com.dayu.share.service.IShareGoodsBrandService;
import com.dayu.common.utils.poi.ExcelUtil;
import com.dayu.common.core.page.TableDataInfo;

/**
 * 商品品牌Controller
 * 
 * @author HK
 * @date 2021-04-08
 */
@RestController
@RequestMapping("/share/brand")
public class ShareGoodsBrandController extends BaseController
{
    @Autowired
    private IShareGoodsBrandService shareGoodsBrandService;
    @Autowired
    private IShareGoodsService goodsService;

    /**
     * 查询商品品牌列表
     */
    @PreAuthorize("@ss.hasPermi('share:brand:list')")
    @GetMapping("/list")
    public TableDataInfo list(ShareGoodsBrand shareGoodsBrand)
    {
        startPage();
        List<ShareGoodsBrand> list = shareGoodsBrandService.selectShareGoodsBrandList(shareGoodsBrand);
        return getDataTable(list);
    }

    /**
     * 导出商品品牌列表
     */
    @PreAuthorize("@ss.hasPermi('share:brand:export')")
    @Log(title = "商品品牌", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ShareGoodsBrand shareGoodsBrand)
    {
        List<ShareGoodsBrand> list = shareGoodsBrandService.selectShareGoodsBrandList(shareGoodsBrand);
        ExcelUtil<ShareGoodsBrand> util = new ExcelUtil<ShareGoodsBrand>(ShareGoodsBrand.class);
        return util.exportExcel(list, "brand");
    }

    /**
     * 获取商品品牌详细信息
     */
    @PreAuthorize("@ss.hasPermi('share:brand:query')")
    @GetMapping(value = "/{brandId}")
    public AjaxResult getInfo(@PathVariable("brandId") Long brandId)
    {
        return AjaxResult.success(shareGoodsBrandService.selectShareGoodsBrandById(brandId));
    }

    /**
     * 新增商品品牌
     */
    @PreAuthorize("@ss.hasPermi('share:brand:add')")
    @Log(title = "商品品牌", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ShareGoodsBrand shareGoodsBrand)
    {
        return toAjax(shareGoodsBrandService.insertShareGoodsBrand(shareGoodsBrand));
    }

    /**
     * 修改商品品牌
     */
    @PreAuthorize("@ss.hasPermi('share:brand:edit')")
    @Log(title = "商品品牌", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ShareGoodsBrand shareGoodsBrand)
    {
        return toAjax(shareGoodsBrandService.updateShareGoodsBrand(shareGoodsBrand));
    }

    /**
     * 删除商品品牌
     */
    @PreAuthorize("@ss.hasPermi('share:brand:remove')")
    @Log(title = "商品品牌", businessType = BusinessType.DELETE)
	@DeleteMapping("/{brandIds}")
    public AjaxResult remove(@PathVariable Long[] brandIds)
    {
        for (Long id : brandIds) {
            if(id == 1) {
                return AjaxResult.error("苹果在保属于系统数据，不能删除！");
            }
            if(id == 2) {
                return AjaxResult.error("苹果过保属于系统数据，不能删除！");
            }
            ShareGoods shareGoods = new ShareGoods();
            shareGoods.setBrandId(id);
            List<ShareGoods> list = goodsService.selectShareGoodsList(shareGoods);
            if(list.size() > 0) {
                return AjaxResult.error("品牌下有数据，不能删除！");
            }
        }
        return toAjax(shareGoodsBrandService.deleteShareGoodsBrandByIds(brandIds));
    }
}
